<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="h_top">
          <div class="bread_title">
            <span>更新计划性维护工单 {{data.status}}</span>
          </div>
          <div class="save_button">
            <div v-if="canBz" class="buttonBox">
              <el-button type="danger" icon="el-icon-close" size="mini" @click="bzBackHandle">班长退回</el-button>
              <el-button type="success" icon="el-icon-circle-check" size="mini" @click="bzHandle">班长确认</el-button>
            </div>
            <div v-if="canEngineer" class="buttonBox">
              <el-button type="danger" icon="el-icon-close" size="mini" @click="engineerBackHandle">工程师退回</el-button>
              <el-button type="success" icon="el-icon-circle-check" size="mini" @click="engineerHandle">工程师确认</el-button>
            </div>
            <div v-if="data.status === -1" class="buttonBox">
              <el-button type="success" icon="el-icon-document-checked" size="mini" @click="handOut">派发</el-button>
            </div>
            <div v-if="isOnce || !zy" class="buttonBox">
              <el-button type="primary" icon="el-icon-document-checked" size="mini" @click="save">保存</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-remove" size="mini" @click="confirmDel">删除</el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-row>
        <el-col :span="24">
          <el-form ref="form" :model="data" :inline="true" :rules="formRules">
            <el-input v-model="data.id" type="hidden" />
            <el-row :gutter="10">
              <el-col :span="4">
                <el-form-item prop="coding">
                  <div>编号</div>
                  <el-input v-model="data.coding" size="mini" :disabled="!isOnce" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="name">
                  <div>名称</div>
                  <el-input v-model="data.name" size="mini" :disabled="!isOnce" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="admin_id">
                  <div>负责人</div>
                  <v-select v-model="data.admin_id" :disabled="!isOnce" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item>
                  <div>电话</div>
                  <el-input v-model="data.admin.phone" disabled size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="profession">
                  <div>专业</div>
                  <el-select v-model="data.profession" size="mini" :disabled="!isOnce">
                    <el-option v-for="(item, index) in profession" :key="index" :label="item" :value="index" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="2">
                <el-form-item prop="type">
                  <div>类型</div>
                  <el-select v-model="data.type" size="mini" :disabled="!isOnce">
                    <el-option v-for="(item, index) in groupType" :key="index" :value="index" :label="item" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="2">
                <el-form-item prop="unit">
                  <div>单位</div>
                  <el-select v-model="data.unit" size="mini" :disabled="!isOnce">
                    <el-option v-for="(item,index) in planType" :key="index" :label="item" :value="index" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="equipment_id">
                  <div>资产</div>
                  <v-select-s v-model="data.equipment_id" action="eqList" :query="{type:data.type,unit:data.unit}" :disabled="!isOnce" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="arrange_date">
                  <div>安排时间</div>
                  <el-date-picker v-model="data.arrange_date" type="datetime" placeholder="选择日期时间" align="right" value-format="yyyy-MM-dd HH:mm:ss" size="mini" :disabled="zy" />
                </el-form-item>
              </el-col>
              <el-col v-if="isOnce || giveOut || isAdmin" :span="2">
                <el-form-item prop="is_send">
                  <div>是否发送给负责人</div>
                  <el-select v-model="data.is_send" size="mini" :disabled="giveOut">
                    <el-option v-for="(item,index) in planSend" :key="index" :label="item" :value="index" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col v-if="isOnce || giveOut || isAdmin" :span="2">
                <el-form-item prop="bz_check">
                  <div>是否班长审核</div>
                  <el-select v-model="data.bz_check" size="mini" :disabled="giveOut">
                    <el-option v-for="(item,index) in planSend" :key="index" :label="item" :value="index" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col v-if="isOnce || giveOut || isAdmin" :span="2">
                <el-form-item prop="engineer_check">
                  <div>是否工程师审核</div>
                  <el-select v-model="data.engineer_check" size="mini" :disabled="giveOut">
                    <el-option v-for="(item,index) in planSend" :key="index" :label="item" :value="index" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col v-if="isOnce || giveOut" :span="4">
                <el-form-item prop="out_id">
                  <div>委外工单</div>
                  <v-select v-model="data.out_id" action="outList" :disabled="giveOut" />
                </el-form-item>
              </el-col>
              <el-col v-if="!isOnce && !giveOut" :span="4">
                <el-form-item prop="apply_id">
                  <div>维护计划</div>
                  <v-select v-model="data.plan_id" action="planList" :disabled="!isOnce" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="sort">
                  <div>排序</div>
                  <el-input v-model="data.sort" size="mini" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="10">
              <el-col v-if="data.complete_date" :span="4">
                <div class="mb10">完成时间</div>
                <el-date-picker v-model="data.complete_date" type="datetime" align="right" value-format="yyyy-MM-dd HH:mm:ss" size="mini" disabled />
              </el-col>
              <el-col v-if="data.wxBz" :span="4">
                <div class="mb10">班长</div>
                <el-input v-model="data.bz_admin.name" size="mini" disabled />
              </el-col>
              <el-col v-if="data.wxBz_date" :span="4">
                <div class="mb10">班长确认时间</div>
                <el-date-picker v-model="data.wxBz_date" type="datetime" align="right" value-format="yyyy-MM-dd HH:mm:ss" size="mini" disabled />
              </el-col>
              <el-col v-if="data.engineer" :span="4">
                <div class="mb10">工程师</div>
                <el-input v-model="data.engineer_admin.name" size="mini" disabled />
              </el-col>
              <el-col v-if="data.engineer_date" :span="4">
                <div class="mb10">工程师确认时间</div>
                <el-date-picker v-model="data.engineer_date" type="datetime" align="right" value-format="yyyy-MM-dd HH:mm:ss" size="mini" disabled />
              </el-col>
            </el-row>
          </el-form>
          <el-tabs>
            <el-tab-pane label="详情">
              <template>
                <el-table :data="eqList">
                  <el-table-column label="资产" min-width="20%">
                    <template scope="scope">
                      <div v-if="scope.row.eq.admin">
                        {{ scope.row.eq.name }} - {{ scope.row.eq.coding }} - {{ scope.row.eq.admin.name }}
                      </div>
                      <div v-else>
                        {{ scope.row.eq.name }} - {{ scope.row.eq.coding }}
                      </div>
                    </template>
                  </el-table-column>
                  <el-table-column label="任务" min-width="20%" prop="task.name" />
                  <el-table-column label="参数" min-width="15%" prop="param.name" />
                  <el-table-column label="读数" min-width="20%">
                    <template scope="scope">
                      <div v-if="scope.row.param.category === 3">
                        <el-radio v-model="scope.row.reading" label="1">正常</el-radio>
                        <el-radio v-model="scope.row.reading" label="2">不正常</el-radio>
                      </div>
                      <div v-else>
                        <el-input v-model="scope.row.reading" />
                      </div>
                    </template>
                  </el-table-column>
                  <el-table-column label="执行者" min-width="10%" prop="admin.name" />
                  <el-table-column label="执行时间" min-width="15%" prop="update_time" />
                </el-table>
                <el-button v-if="this.data.status === 0" type="primary" icon="el-icon-circle-check" size="mini" class="submit-reading" @click="submitReading">提交</el-button>
              </template>
            </el-tab-pane>
            <el-tab-pane label="员工">
              <template>
                <el-row>
                  <el-col :span="24">
                    <el-table :data="staffList">
                      <el-table-column label="专业" prop="profession" min-width="24%" :formatter="professionFormat" />
                      <el-table-column label="计划的工时" prop="man_hour" min-width="34%" />
                      <el-table-column label="偏好的员工" prop="admin.name" min-width="34%" />
                      <el-table-column label="操作" min-width="8%">
                        <template scope="scope">
                          <div v-if="data.status === -1">
                            <div v-if="isOnce || isAdmin">
                              <el-button type="text" size="small" @click="removeStaff(scope.row.id)">删除</el-button>
                            </div>
                          </div>
                          <div v-else>
                            <span>-</span>
                          </div>
                        </template>
                      </el-table-column>
                      <div v-if="this.data.status === -1" slot="append">
                        <el-form ref="append_staff" :model="staffData" inline class="hf_form append_other_admin" :rules="append_staff">
                          <el-row :gutter="10">
                            <el-col :span="6">
                              <el-form-item prop="profession">
                                <el-select v-model="staffData.profession" size="mini">
                                  <el-option v-for="(item, index) in profession" :key="index" :value="index" :label="item" />
                                </el-select>
                              </el-form-item>
                            </el-col>
                            <el-col :span="8">
                              <el-form-item prop="hours">
                                <el-input v-model="staffData.hours" size="mini" />
                              </el-form-item>
                            </el-col>
                            <el-col :span="8">
                              <el-form-item prop="admin_id">
                                <v-select-s v-model="staffData.admin_id" />
                              </el-form-item>
                            </el-col>
                            <el-col :span="2">
                              <el-form-item>
                                <el-button icon="el-icon-upload2" size="mini" @click="appendStaff" />
                              </el-form-item>
                            </el-col>
                          </el-row>
                        </el-form>
                      </div>
                    </el-table>
                  </el-col>
                </el-row>
              </template>
            </el-tab-pane>
            <el-tab-pane label="备件">
              <template>
                <el-row>
                  <el-col :span="24">
                    <el-table :data="parts.data">
                      <el-table-column label="备品备件" prop="parts.name" min-width="46%">
                        <template scope="scope">
                          <span v-if="scope.row.already === 2" style="color: green">{{ scope.row.parts.name }}</span>
                          <span v-else-if="scope.row.already === 1" style="color: blue">{{ scope.row.parts.name }}</span>
                          <span v-else>{{ scope.row.parts.name }}</span>
                        </template>
                      </el-table-column>
                      <el-table-column label="数量" prop="number" min-width="46%">
                        <template slot-scope="scope">
                          <span v-if="scope.row.isSet">
                            <el-input v-model="editNumber" size="mini" placeholder="请输入数量" />
                          </span>
                          <span v-else>
                            {{ scope.row.number }}
                          </span>
                        </template>
                      </el-table-column>
                      <el-table-column label="操作" min-width="8%">
                        <template v-slot="scope">
                          <!--<div v-if="isOnce">-->
                          <div v-if="isAdmin">
                            <div v-if="scope.row.already !== 2">
                              <span class="el-tag el-tag--info el-tag--mini cur" @click="editOrCancelPartsHandle(scope.row,scope.$index,true)">
                                {{ scope.row.isSet?'保存':"修改" }}
                              </span>
                              <span v-if="!scope.row.isSet" class="el-tag el-tag--danger el-tag--mini cur" @click="removePartsHandle(scope.row.id)">
                                删除
                              </span>
                              <span v-else class="el-tag el-tag--mini cur" @click="editOrCancelPartsHandle(scope.row,scope.$index,false)">
                                取消
                              </span>
                            </div>
                            <div v-else>-</div>
                          </div>
                          <div v-else>
                            <span>-</span>
                          </div>
                        </template>
                      </el-table-column>
                      <div v-if="isOnce" slot="append">
                        <el-form ref="append_parts" :model="partsData" inline class="hf_form append_other_admin" :rules="append_parts">
                          <el-row :gutter="10">
                            <el-col :span="11">
                              <el-form-item prop="part_id">
                                <v-select v-model="partsData.part_id" action="partsList" />
                              </el-form-item>
                            </el-col>
                            <el-col :span="11">
                              <el-form-item prop="number">
                                <el-input v-model="partsData.number" size="mini" />
                              </el-form-item>
                            </el-col>
                            <el-col :span="2">
                              <el-form-item>
                                <el-button icon="el-icon-upload2" size="mini" @click="appendParts" />
                              </el-form-item>
                            </el-col>
                          </el-row>
                        </el-form>
                      </div>
                    </el-table>
                  </el-col>
                </el-row>
                <el-row v-if="this.data.status === 0 && this.partsData.length > 0">
                  <el-button type="primary" size="mini" class="submit-reading" @click="getParts">请求</el-button>
                </el-row>
              </template>
            </el-tab-pane>
            <el-tab-pane label="描述">
              <el-row>
                <el-col :span="24">
                  <Tinymce id="content" v-model="data.content" />
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="任务" v-if="isOnce">
              <template>
                <el-table :data="taskList">
                  <el-table-column label="任务" min-width="29%" prop="task.name" />
                  <el-table-column label="参数" min-width="29%" prop="param.name" />
                  <el-table-column label="细节" min-width="34%" prop="detail" />
                  <el-table-column label="操作" min-width="8%">
                    <template scope="scope">
                      <div v-if="isOnce">
                        <el-button type="text" size="small" @click="removeTask(scope.row.id)">删除</el-button>
                      </div>
                      <div v-else>
                        <span>-</span>
                      </div>
                    </template>
                  </el-table-column>
                  <div slot="append">
                    <el-form ref="append_task" :model="taskData" inline class="hf_form append_other_admin" :rules="append_task">
                      <el-input v-model="data.id" type="hidden" />
                      <el-row :gutter="10">
                        <el-col :span="7">
                          <el-form-item prop="task_id">
                            <v-select v-model="taskData.task_id" action="taskList" />
                          </el-form-item>
                        </el-col>
                        <el-col :span="7">
                          <el-form-item prop="param_id">
                            <v-select v-model="taskData.param_id" action="paramList" />
                          </el-form-item>
                        </el-col>
                        <el-col :span="8">
                          <el-form-item prop="detail">
                            <el-input v-model="taskData.detail" size="mini" />
                          </el-form-item>
                        </el-col>
                        <el-col :span="2">
                          <el-form-item>
                            <el-button icon="el-icon-upload2" size="mini" @click="appendTask" />
                          </el-form-item>
                        </el-col>
                      </el-row>
                    </el-form>
                  </div>
                </el-table>
                <el-row style="margin: 10px 0;">
                  <el-button>选择任务组</el-button>
                </el-row>
                <el-row :gutter="10">
                  <el-form ref="append_taskGroup" :model="taskGroupData" inline class="hf_form append_other_admin" :rules="append_taskGroup">
                    <el-col :span="7">
                      <el-form-item prop="task_group_id">
                        <v-select v-model="taskGroupData.task_group_id" action="taskGroupList" :query="{'status': 0, 'add_field': 'coding'}" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="2">
                      <el-form-item>
                        <el-button icon="el-icon-upload2" size="mini" @click="appendTaskGroup" />
                      </el-form-item>
                    </el-col>
                  </el-form>
                </el-row>
              </template>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import API from '@/api/maintain/pm'
import { applyType, applyMode, profession, planType, planSend, groupType } from '@/utils/options'
import Tinymce from '@/components/Tinymce/index'
import VSelect from '@/components/select'
import VSelectS from '@/components/select-search'
export default {
  inject: ['backUrl', 'delOne', 'update', 'reload'],
  components: {
    Tinymce,
    VSelect,
    VSelectS
  },
  data() {
    return {
      data: {
        'admin': {
          'phone': ''
        },
        'cr_admin': {
          'name': ''
        },
        'eq': {
          'name': ''
        },
        out_id: ''
      },
      parts: {
        sel: null,
        data: []
      },
      applyType,
      applyMode,
      profession,
      planType,
      groupType,
      planSend,
      isOnce: false,
      zy: true,
      giveOut: false,
      isAdmin: true,
      canBz: false,
      canEngineer: false,
      unexecuted: false,
      staffList: [],
      taskList: [],
      eqList: [],
      staffData: {},
      partsData: {},
      taskData: {},
      taskGroupData: {},
      editNumber: '',
      editId: '',
      formRules: {
        coding: [
          {
            required: true,
            message: '请输入编号',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入工单名称',
            trigger: 'blur'
          }
        ],
        admin_id: [
          {
            required: true,
            message: '请选择负责人',
            trigger: 'change'
          }
        ],
        profession: [
          {
            required: true,
            message: '请选择专业',
            trigger: 'change'
          }
        ],
        sort: [
          {
            required: true,
            message: '请输入排序',
            trigger: 'blur'
          }
        ]
      },
      append_staff: {
        profession: [
          {
            required: true,
            message: '请选择专业',
            trigger: 'change'
          }
        ],
        hours: [
          {
            required: true,
            message: '请填写工时',
            trigger: 'blur'
          }
        ],
        admin_id: [
          {
            required: true,
            message: '请选择员工',
            trigger: 'change'
          }
        ]
      },
      append_task: {
        task_id: [
          {
            required: true,
            message: '请选择任务',
            trigger: 'change'
          }
        ],
        param_id: [
          {
            required: true,
            message: '请选择参数',
            trigger: 'change'
          }
        ]
      },
      append_taskGroup: {
        task_group_id: [
          {
            required: true,
            message: '请选择任务组',
            trigger: 'change'
          }
        ]
      },
      append_parts: {
        part_id: [
          {
            required: true,
            message: '请选择备件',
            trigger: 'change'
          }
        ],
        number: [
          {
            required: true,
            message: '请填写数量',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    const id = this.$route.params.id
    API.getOne(id).then(response => {
      if (response.data.error_code) {
        this.$alert(response.data.message, '错误', {
          confirmButtonText: '确定',
          callback: () => {
            this.backUrl()
          }
        })
      }

      this.data = response.data
      this.staffList = response.data.other_member
      this.taskList = response.data.other_task
      this.parts.data = response.data.other_parts
      this.eqList = response.data.other_eq
      if (!response.data.plan_id) {
        this.isOnce = true
      }
      if (response.data.plan_id === -1) {
        this.giveOut = true
      }
      // 仅班长确认
      if (response.data.bz_check === 1 && response.data.engineer_check === 0) {
        if (response.data.status === 1) {
          this.canBz = true
        }
      }
      // 仅工程师确认
      if (response.data.bz_check === 0 && response.data.engineer_check === 1) {
        if (response.data.status === 1) {
          this.canEngineer = true
        }
      }
      // 班长和工程师都需要确认
      if (response.data.bz_check === 1 && response.data.engineer_check === 1) {
        if (response.data.status === 1) {
          this.canBz = true
        } else if (response.data.status === 2) {
          this.canEngineer = true
        }
      }
      console.log(response.data.plan_id)
      if (response.data.plan_category === 2 || !response.data.plan_id) {
        this.zy = false
      }
    })
  },
  methods: {
    confirmDel() {
      const id = this.data.id
      this.delOne(API, id, true)
    },
    save() {
      const thisData = this.data
      const thisForm = this.$refs.form
      this.update(API, thisData, thisForm)
    },
    handOut() {
      const id = this.data.id
      API.giveOut(id).then((response) => {
        if (response.data.error_code === 1) {
          this.$message({
            message: '恭喜你，派发成功',
            type: 'success'
          })
          this.reload()
        } else {
          this.$message.error(response.data.message)
        }
      })
    },
    appendParts() {
      this.$refs.append_parts.validate((valid) => {
        if (valid) {
          const part_id = this.partsData.part_id
          const number = this.partsData.number
          const parts_data = {
            'parts_id': part_id,
            'number': number,
            'work_order_id': this.$route.params.id
          }
          API.appendParts(parts_data).then(response => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              API.getOne(this.$route.params.id).then(response => {
                this.parts.data = response.data.other_parts
              })
            } else if (response.data.error_code === 62004) {
              this.$message.error('该备件已存在')
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    removePartsHandle(id) {
      const parts_data = {
        'id': id
      }
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.removeParts(parts_data).then((response) => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            API.getOne(this.$route.params.id).then(response => {
              this.parts.data = response.data.other_parts
            })
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    editOrCancelPartsHandle(row, index, cg) {
      // 点击修改 判断是否已经保存所有操作
      for (const i of this.parts.data) {
        if (i.isSet && i.id !== row.id) {
          this.$message.warning('请先保存当前编辑项')
          return false
        }
      }
      // 是否是取消操作
      if (!cg) {
        if (!this.parts.sel.id) {
          this.parts.data.splice(index, 1)
        }
        return this.$set(this.parts.data[index], 'isSet', false)
      }
      // 提交数据
      const _this = this
      if (row.isSet) {
        (function() {
          const edit_parts_data = {
            'number': _this.editNumber,
            'id': _this.editId
          }
          API.editParts(edit_parts_data).then(response => {
            if (response.data.error_code === 1) {
              _this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              API.getOne(_this.$route.params.id).then((res) => {
                _this.parts.data = res.data.other_parts
              })
            } else {
              this.$message.error(response.data.message)
            }
          })
        })()
      } else {
        this.parts.sel = JSON.parse(JSON.stringify(row))
        this.$set(this.parts.data[index], 'isSet', true)
        this.editNumber = this.parts.data[index].number
        this.editId = this.parts.data[index].id
      }
    },
    appendStaff() {
      this.$refs.append_staff.validate((valid) => {
        if (valid) {
          const profession = this.staffData.profession
          const hours = this.staffData.hours
          const admin_id = this.staffData.admin_id
          const admin_data = {
            'profession': profession,
            'hours': hours,
            'admin_id': admin_id,
            'work_order_id': this.$route.params.id
          }
          API.appendMember(admin_data).then(response => {
            console.log(response)
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              API.getOne(this.$route.params.id).then(response => {
                this.staffList = response.data.other_member
              })
            } else if (response.data.error_code === 62004) {
              this.$message.error('该人员已存在')
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    removeStaff(id) {
      const admin_data = {
        'id': id
      }
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.removeMember(admin_data).then((response) => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            API.getOne(this.$route.params.id).then(response => {
              this.staffList = response.data.other_member
            })
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    appendTask() {
      this.$refs.append_task.validate((valid) => {
        if (valid) {
          const task_id = this.taskData.task_id
          const param_id = this.taskData.param_id
          const detail = this.taskData.detail
          const task_data = {
            'task_id': task_id,
            'param_id': param_id,
            'detail': detail,
            'work_order_id': this.$route.params.id
          }
          API.appendTask(task_data).then(response => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              API.getOne(this.$route.params.id).then(response => {
                this.taskList = response.data.other_task
              })
            } else if (response.data.error_code === 62004) {
              this.$message.error('该任务已存在')
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    removeTask(id) {
      const task_data = {
        'id': id
      }
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.removeTask(task_data).then((response) => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            API.getOne(this.$route.params.id).then(response => {
              this.taskList = response.data.other_task
            })
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    appendTaskGroup() {
      this.$refs.append_taskGroup.validate((valid) => {
        if (valid) {
          const task_group = this.taskGroupData.task_group_id
          const task_data = {
            'task_group': task_group,
            'work_order_id': this.$route.params.id
          }
          API.appendTaskGroup(task_data).then(response => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              API.getOne(this.$route.params.id).then(response => {
                this.taskList = response.data.other_task
              })
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    professionFormat(row) {
      if (row.profession === 0) {
        return '所有'
      } else if (row.profession === 1) {
        return 'ELE - 电气'
      } else if (row.profession === 2) {
        return 'MEC - 机械'
      }
    },
    getParts() {
      const needData = {
        id: this.data.id
      }
      API.needParts(needData).then(response => {
        if (response.data.error_code === 1) {
          this.$message.success('请求备件，请速去库房领件！')
          this.backUrl()
        } else {
          this.$message.error(response.data.message)
        }
      })
    },
    submitReading() {
      API.submitReading(this.eqList).then((response) => {
        if (response.data.error_code === 1) {
          this.$message({
            message: '恭喜你，提交成功',
            type: 'success'
          })
          API.getOne(this.$route.params.id).then(response => {
            this.eqList = response.data.other_eq
          })
        } else if (response.data.error_code === 11) {
          this.$message({
            message: '参数不正常，已自动生成报修单',
            type: 'success'
          })
          API.getOne(this.$route.params.id).then(response => {
            this.eqList = response.data.other_eq
          })
        } else {
          this.$message.error(response.data.message)
        }
      })
    },
    finishHandle() {
      const reviewData = {
        'status': 1,
        'content': this.data.content,
        'id': this.$route.params.id
      }
      API.review(reviewData).then((response) => {
        if (response.data.error_code === 1) {
          this.$message({
            message: '恭喜你，提交成功',
            type: 'success'
          })
          this.reload()
        } else {
          this.$message.error(response.data.message)
        }
      })
    },
    bzHandle() {
      const reviewData = {
        'status': 2,
        'content': this.data.content,
        'id': this.$route.params.id
      }
      API.review(reviewData).then((response) => {
        if (response.data.error_code === 1) {
          this.$message({
            message: '恭喜你，提交成功',
            type: 'success'
          })
          this.reload()
        } else {
          this.$message.error(response.data.message)
        }
      })
    },
    bzBackHandle() {
      const reviewData = {
        'status': 21,
        'content': this.data.content,
        'id': this.$route.params.id
      }
      API.review(reviewData).then((response) => {
        if (response.data.error_code === 1) {
          this.$message({
            message: '恭喜你，提交成功',
            type: 'success'
          })
          this.reload()
        } else {
          this.$message.error(response.data.message)
        }
      })
    },
    engineerHandle() {
      const reviewData = {
        'status': 3,
        'content': this.data.content,
        'id': this.$route.params.id
      }
      API.review(reviewData).then((response) => {
        if (response.data.error_code === 1) {
          this.$message({
            message: '恭喜你，提交成功',
            type: 'success'
          })
          this.reload()
        } else {
          this.$message.error(response.data.message)
        }
      })
    },
    engineerBackHandle() {
      const reviewData = {
        'status': 22,
        'content': this.data.content,
        'id': this.$route.params.id
      }
      API.review(reviewData).then((response) => {
        if (response.data.error_code === 1) {
          this.$message({
            message: '恭喜你，提交成功',
            type: 'success'
          })
          this.reload()
        } else {
          this.$message.error(response.data.message)
        }
      })
    }
  }
}
</script>

<style>
.submit-reading{margin: 10px 0 0 10px;}
</style>
